package teach.aistar.rbm;

import com.rabbitmq.client.Channel;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.support.AmqpHeaders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.messaging.handler.annotation.Headers;
import org.springframework.stereotype.Component;
import teach.aistar.mapper.EmailMapper;
import teach.aistar.service.IEmailService;

import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@Component
public class Email {

    @Autowired
    private JavaMailSender javaMailSender;

//    @Autowired
//    private Red

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private IEmailService iEmailService;

    @Autowired
    private EmailMapper emailMapper;
//    @Autowired
//    private JedisCluster  jedisCluster;
     @RabbitListener(queues = "insertemail")
    @RabbitHandler
     public void insert(String email, @Headers Map<String,Object> headers, Channel channel)
     {

//         System.out.println("正在插入数据库中。。。。。。");

           List<teach.aistar.model.entity.Email> emails = emailMapper.selectE(email);
           if(emails.size()==0)
           {
               teach.aistar.model.entity.Email email1 = new teach.aistar.model.entity.Email();
               email1.setEmail(email);
               iEmailService.insertE(email1);
           }
      }

    @RabbitListener(queues = "email")
    @RabbitHandler
    public void direct(String email, @Headers Map<String,Object> headers, Channel channel){

        SimpleMailMessage msg = new SimpleMailMessage();

        //邮箱的发送方 - 配置文件中的username保持一致
        msg.setFrom("1849730314@qq.com");


        //邮箱的标题
        msg.setSubject("龙腾有限公司:");




        //邮箱的正文内容
        //验证码随机数 - 6位(Math.random()*10);
        String code = String.valueOf((int) (Math.random()*900000+100000));


        msg.setText("尊敬的用户：欢迎您使用邮箱进行游客登录，您的验证码是："+ code +"，请在一分钟内进行验证哦");


//        ValueOperations vop = redisTemplate.opsForValue();
//        vop.set("user_message", code);
//
//        String user_message = (String) vop.get("user_message");
//
//        System.out.println("user_message........"+user_message);
        msg.setTo(email);

        //发送
        javaMailSender.send(msg);


        System.out.println("发送成功");

        ValueOperations vop = redisTemplate.opsForValue();
        vop.set("code_"+email,code);
        redisTemplate.expire("code_"+email,10, TimeUnit.SECONDS);




        long ack = (long) headers.get(AmqpHeaders.DELIVERY_TAG);
        try {
            channel.basicAck(ack,false);
        } catch (IOException e) {
            e.printStackTrace();
        }


    }

}
